package com.xjnt.config;

import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.xjnt.base.support.admin.interceptor.ExceptionAndLogInterceptor;
import com.xjnt.base.support.admin.shiro.jdbc.ShiroAuthzLoader;
import com.xjnt.frameworks.interceptor.InjectInterceptor;
import com.xjnt.frameworks.plugin.entity.EntityPlugin;
import com.xjnt.frameworks.plugin.entity.naming.SimpleNameStyles;
import com.xjnt.frameworks.plugin.log.Slf4jLogFactory;
import com.xjnt.frameworks.plugin.quartz.QuartzPlugin;
import com.xjnt.frameworks.plugin.route.AutoRoutes;
import com.xjnt.frameworks.plugin.service.ServicePlugin;
import com.xjnt.frameworks.plugin.shiro.ShiroPlugin;
import com.xjnt.frameworks.plugin.shiro.interceptor.ShiroInterceptor;
import com.xjnt.frameworks.plugin.sql.SqlPlugin;
import com.xjnt.frameworks.web.handler.SessionHandler;

public class GlobalConfig extends JFinalConfig {

	private InitialConfig config;
	
	private Routes routes;

	@Override
	public void configConstant(Constants me) {
		/* 初始化配置 */
		config = new InitialConfig(this);
		/* 设置日志输出 */
		Logger.setLoggerFactory(new Slf4jLogFactory());
		
		me.setDevMode(config.getDevMode());
		me.setBaseViewPath(config.getViewPath());
		/* 设置默认渲染引擎为 Beetl */
		me.setMainRenderFactory(new BeetlRenderFactory());
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AutoRoutes().addScanPackages(config.getControllerScanPkg()).addIncludeJars(config.getControllerScanJar()));
		this.routes = me;
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin drp = config.getDruidPlugin();
		drp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter(); // 防注入
		wall.setDbType("mysql");
		drp.addFilter(wall);
		me.add(drp);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin("MySql", drp).setDevMode(config.getDevMode()).setDialect(new MysqlDialect()).setShowSql(config.getShowSql());
		//设置事物级别为4
		arp.setTransactionLevel(4);
		EntityPlugin entityPlugin = new EntityPlugin(arp).setNameStyle(SimpleNameStyles.DEFAULT).addScanPackages(config.getModelScanPkg()).addIncludeJars(config.getModelScanJar());
		me.add(entityPlugin);
		me.add(arp);
		//业务层加载
		me.add(new ServicePlugin().addScanPackages(config.getServiceScanPkg()).addIncludeJars(config.getServiceScanJar()));
		//自定义SQL加载
		me.add(new SqlPlugin());
		//任务调度器加载
		me.add(new QuartzPlugin().addScanPackages(config.getQuartzJobScanPkg()).addIncludeJars(config.getQuartzJobScanJar()));
		//Shiro权限加载
		me.add(new ShiroPlugin(routes, new ShiroAuthzLoader()));
		//Ehcache 缓存配置
		me.add(new EhCachePlugin());

	}

	@Override
	public void configInterceptor(Interceptors me) {
		/* Shiro 权限拦截器 */
		me.add(new ShiroInterceptor());
		/* 业务服务层注入拦截器 */
		me.add(new InjectInterceptor());
		/* 异常/日志拦截器 */
		me.add(new ExceptionAndLogInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new SessionHandler());
		me.add(new DruidStatViewHandler("/system/monitor/druid/res"));
	}

}
